package org.example.yongyou;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.type.TypeReference;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

public class 获取token_BIP {

    public static void main(String[] args) throws IOException, NoSuchAlgorithmException, InvalidKeyException {
        //获取域名
        String tenantId = "h7uckxnv";
        String appKey = "c48ce65130f74e2fbd669c0a584e8bca";
        String appSecret = "6e54ee39706a882b01425ef419f048e4971c3acd";

        String url = "https://apigateway.yonyoucloud.com/open-auth/dataCenter/getGatewayAddress?tenantId=" + tenantId;
        HttpRequest get = HttpUtil.createGet(url);
        String body = get.execute().body();

        JSONObject jsonObject = JSONUtil.parseObj(body);
        JSONObject data = (JSONObject) jsonObject.get("data");
        System.out.println("data.get(\"gatewayUrl\") = " + data.get("gatewayUrl"));
        System.out.println("data.get(\"tokenUrl\") = " + data.get("tokenUrl"));

        String requestUrl = data.get("tokenUrl") + "/open-auth/selfAppAuth/getAccessToken";
        Map<String, String> params = new HashMap<>();
        params.put("appKey", appKey);
        params.put("timestamp", String.valueOf(System.currentTimeMillis()));
        String signature = SignHelper.sign(params, appSecret);
        params.put("signature", signature);
        GenericResponse<AccessTokenResponse> response = RequestTool.doGet(requestUrl, params, new TypeReference<GenericResponse<AccessTokenResponse>>() {
        });
        if (response.isSuccess()) {
            System.out.println("response.getData().getAccessToken() = " + response.getData().getAccessToken());
        }


    }

}
